<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
			<el-form-item label="主键" prop="id" v-if="false">
				<el-input v-model="dataForm.id" placeholder="主键"></el-input>
			</el-form-item>

			<!-- <el-row>
				<el-col :span="12">
				
				</el-col>
				<el-col :span="12">
				
				</el-col>
			</el-row> -->

			<el-row>
				<el-col :span="12">
					<el-form-item label="资产名称" prop="name">
						<el-input v-model="dataForm.name" placeholder="资产名称"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="资产类别" prop="typeId">
						<el-tree-select
							v-model="dataForm.typeId"
							:data="ziChanList"
							value-key="id"
							check-strictly
							:render-after-expand="false"
							:props="{ value: 'id', label: 'name', children: 'children' }"
							style="width: 100%"
						/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="12">
					<el-form-item label="资产编号" prop="assetNo">
						<el-input v-model="dataForm.assetNo" placeholder="资产编号"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="规格型号" prop="model">
						<el-input v-model="dataForm.model" placeholder="规格型号"></el-input>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="12">
					<el-form-item label="计量单位" prop="units">
						<el-input v-model="dataForm.units" placeholder="计量单位"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="序列号" prop="cdkey">
						<el-input v-model="dataForm.cdkey" placeholder="序列号"></el-input>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="12">
					<el-form-item label="资产来源" prop="sourceId">
						<el-tree-select
							v-model="dataForm.sourceId"
							:data="laiYuanList"
							value-key="id"
							check-strictly
							:render-after-expand="false"
							:props="{ value: 'id', label: 'name', children: 'children' }"
							style="width: 100%"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="购入日期" prop="buyDate">
						<ElDatePicker v-model="dataForm.buyDate" placeholder="购入日期" style="width:100%"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="12">
					<el-form-item label="所属部门" prop="theirDeptId">
						<el-tree-select
							v-model="dataForm.theirDeptId"
							:data="deptList"
							value-key="id"
							check-strictly
							:render-after-expand="false"
							:props="{ value: 'id', label: 'name', children: 'children' }"
							style="width: 100%"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="购入价格" prop="buyPrice">
						<el-input v-model="dataForm.buyPrice" placeholder="购入价格">
							<template #append>元</template>
						</el-input>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row>
				<el-col :span="12">
					<el-form-item label="使用部门" prop="useDeptId">
						<el-tree-select
							v-model="dataForm.useDeptId"
							:data="deptList"
							value-key="id"
							check-strictly
							:render-after-expand="false"
							:props="{ value: 'id', label: 'name', children: 'children' }"
							style="width: 100%"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="使用人" prop="staffId">
						<el-input v-model="dataForm.staffId" placeholder="使用人"></el-input>
					</el-form-item>
				</el-col>
			</el-row>

			<el-form-item label="存放仓库" prop="stowageId">
				<el-tree-select
					v-model="dataForm.stowageId"
					:data="storageList"
					value-key="id"
					check-strictly
					:render-after-expand="false"
					:props="{ value: 'id', label: 'name', children: 'children' }"
					style="width: 100%"
				/>
			</el-form-item>
			<el-form-item label="附件" prop="fileUrl">
				<UploadImgView v-model="dataForm.fileUrl"></UploadImgView>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="dataForm.remark" placeholder="备注"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { Ref, reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useAssetApi, useAssetSubmitApi } from '@/api/nb/asset/asset'
import { findListByDictId } from '@/api/nb/business/dictionaries_subset'
import { findListAll } from '@/api/nb/asset/storage'
import UploadImgView from '@/views/nb_module/image/upload-img-view.vue'

const emit = defineEmits(['refreshDataList'])
defineProps(['deptList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	id: '',

	name: '',
	typeId: '',
	assetNo: '',
	model: '',
	units: '',
	cdkey: '',
	sourceId: '',
	buyDate: '',
	theirDeptId: '',
	buyPrice: '',
	useDeptId: '',
	staffId: '',
	stowageId: '',
	fileUrl: '',
	remark: ''
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getAsset(id)
	}
}

const getAsset = (id: number) => {
	useAssetApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const ziChanList = ref()
const laiYuanList = ref()
const storageList = ref()
const getStorage = () => {
	findListAll().then(res => {
		storageList.value = res.data
	})
}

const getDictList = (code: String, list: Ref) => {
	findListByDictId({ params: { code: code } }).then(res => {
		list.value = res.data
	})
}
getDictList('ZiChanLeiBie', ziChanList)
getDictList('ZiChanLaiYuan', laiYuanList)
getStorage()

const dataRules = ref({})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		useAssetSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
